<?php

namespace app\index\controller;

use app\common\entity\Config;
use app\common\entity\Mywallet;
use app\common\entity\Orders;
use app\common\entity\PersonService;
use app\common\entity\Proportion;
use app\common\entity\RealPass;
use app\common\entity\MywalletLog;
use app\common\entity\UserInviteCode;
use app\common\entity\WithdrawMoney;
use app\common\service\Market\Auth;
use app\common\service\Users\Identity;
use app\index\model\SendCode;
use app\index\validate\RegisterForm;
use Endroid\QrCode\ErrorCorrectionLevel;
use Grafika\Grafika;
use think\facade\Env;
use think\facade\Session;
use think\Request;
use app\common\service\Users\Service;
use app\common\entity\User;
use think\Db;
use app\common\entity\ServiceInfo;
use app\common\entity\Partnerr;
use Grafika\Color;
use app\common\entity\UserLevelSend;
use Endroid\QrCode\QrCode;

class Member extends Base
{

    /**
     * 投诉
     */
    public function Complaint(Request $request)
    {
        $validate = $this->validate($request->post(), '\app\index\validate\Complaint');
        if ($validate !== true) {
            return json(['code' => 1, 'msg' => $validate]);
        }

        $model = new PersonService();
        $add_data = $request->post();
        if(! $this->userId){
            return json()->data(['code'=>1,'msg'=>'请先登录']);
        }

        $add_data['uid'] = $this->userId;
        $res = $model->addNew($model,$add_data);
        if($res){
            return json()->data(['code'=>0,'msg'=>'操作成功']);
        }
        return json()->data(['code'=>1,'msg'=>'操作失败']);
    }


    /**
     * 申诉列表
     * [ComplaintList description]
     */
    public function ComplaintList(){
        $status = input('status',1);
        $limit =input('limit')?input('limit'):15;
        $page =input('page')?input('page'):1;
        $notice = PersonService::where('uid',$this->userId)
            ->where('status',$status)
            ->order('id desc')
            ->page($page,$limit)
            ->select()
            ->toArray();
        foreach ($notice as $key => $value) {
            $notice[$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
        }
        return json(['code' => 0, 'msg' => '请求成功','info'=>$notice]);
    }
    /**
     * [详情]
     * @return
     */
    public function ComplaintListDetail(Request $request){
        $id = $request->post('id');
        $notice = PersonService::where('id',$id)->find();
        return json(['code'=>0,'msg'=>'请求成功','info'=>$notice]);
    }

    #个人中心
    public function index(){

        $info = User::alias('u')
            ->join('my_wallet w','w.user_id = u.id')
            ->field('u.id,u.avatar,u.nick_name,u.mobile,u.level,w.gyg,w.lock,u.is_active,u.real_pass')
            ->where('u.id',$this->userId)
            ->find();
        /*print_r($info);die;*/
        $wallet = Mywallet::where('user_id',$info['id'])->find();
        $info['gyg'] = $wallet['gyg'];
        $info['lock'] = $wallet['lock'];
        $info['user_level'] = UserLevelSend::getCateNameByLevel($info['level']);
        //业绩
        $userList = User::select();
        $temp = self::getTemp($userList,$info['id']);
        $temp_uid = implode(',',array_column($temp,'id'));
        #总业绩
        $info['count'] = Orders::where('user_id',$info['id'])->where('status','>',0)->sum('pay_money');
        #2020/10/19改成团队业绩
        $info['week'] = Orders::where('user_id','in',$temp_uid)->where('status','>',0)->sum('pay_money');
        /*$orderModel = new Orders();
        echo $orderModel->getLastSql();die;*/
        # 2020/10/19改成小区业绩
        $week = Orders::where('user_id','in',$temp_uid)->whereTime('pay_time','week')->where('status','>',0)->sum('pay_money');
        $big = Orders::field('sum(pay_money) as sum_money')
            ->where('user_id','in',$temp_uid)
            ->where('status','in','1,2,3')
            ->whereTime('pay_time','week')
            ->group('user_id')->order('sum_money desc')->find();

        $info['big'] =  bcsub($week,$big['sum_money'],2);
        $info['order_num'] = Orders::where('user_id',$info['id'])->count();
        $info['invite_code'] = UserInviteCode::where('user_id',$this->userId)->value('invite_code');
        return json(['code' => 0, 'info' =>$info]);
    }
    #获取团队
    static public function getTemp($data, $id){
        $list = array();
        foreach($data as $v) {
            if($v['pid'] == $id) {
                $item = self::getTemp($data, $v['id']);
                array_push($list, $v);
                if(!empty($item)) {
                    $list = array_merge($list, $item);
                }
            }
        }
        return $list;
    }
    #设置
    public function set(){
        $info = User::alias('u')
            ->join('my_wallet w','w.user_id = u.id')
            ->field('u.id,u.avatar,u.nick_name,u.mobile,u.level')
            ->where('u.id',$this->userId)
            ->find();
        $info['user_level'] = UserLevelSend::getCateNameByLevel($info['level']);
        $info['invite_code'] = UserInviteCode::getCodeByUserId($this->userId);
        return json(['code' => 0, 'info' =>$info]);
    }
    #修改头像
    public function update_avatar(Request $request){
        $avatar = $request->post('avatar');
        if(!$avatar){
            return json(['code' => 1, 'message' => '请求参数错误']);
        }
        $update = User::where('id',$this->userId)->update(['avatar'=>$avatar,'update_time'=>time()]);
        if($update){
            return json(['code' => 0, 'message' => '操作成功']);
        }
        return json(['code' => 1, 'message' => '操作失败']);
    }
    #修改昵称
    public function update_nickname(Request $request){
        $nick_name = $request->post('nick_name');
        if(!$nick_name){
            return json(['code' => 1, 'message' => '请求参数错误']);
        }
        $update = User::where('id',$this->userId)->update(['nick_name'=>$nick_name,'update_time'=>time()]);
        if($update){
            return json(['code' => 0, 'message' => '操作成功']);
        }
        return json(['code' => 0, 'message' => '操作成功']);
    }
    #修改手机
    public function update_mobile(Request $request){
        $mobile = $request->post('mobile');
//        $code = $request->post('code');
//        if(!$mobile || !$code){
//            return json(['code' => 1, 'message' => '请求参数错误']);
//        }
        if(!$mobile){
            return json(['code' => 1, 'message' => '请求参数错误']);
        }
        $userInfo = User::where('id', $this->userId)->find();
//        $form = new RegisterForm();
//        if (!$form->checkChange($request->post('code'), $userInfo['mobile'])) {
//            return json(['code' => 1, 'message' => '验证码输入错误']);
//        }
        $update = User::where('id',$this->userId)->update(['mobile'=>$mobile]);
        if($update){
            return json(['code' => 0, 'message' => '操作成功']);
        } 
        return json(['code' => 0, 'message' => '操作成功']);
    }



    /**
     * 修改密码
     */
    public function updatePassword(Request $request)
    {
        $validate = $this->validate($request->post(), '\app\index\validate\UpdatePasswordForm');

        if ($validate !== true) {
            return json(['code' => 1, 'message' => $validate]);
        }

        /*$oldPassword = $request->post('old_pwd');*/
        $user = User::where('id', $this->userId)->find();
        $service = new \app\common\service\Users\Service();
        /*$result = $service->checkPassword($oldPassword, $user);

        if (!$result) {
            return json(['code' => 1, 'message' => '原密码输入错误']);
        }*/
        $userInfo = $user;
        $form = new RegisterForm();
        if (!$form->checkChange($request->post('code'), $userInfo['mobile'])) {
           return json(['code' => 1, 'message' => '验证码输入错误']);
        }

        //修改
        $user->password = $service->getPassword($request->post('new_pwd'));

        if ($user->save() === false) {
            return json(['code' => 1, 'message' => '修改失败']);
        }

        return json(['code' => 0, 'message' => '修改成功']);
    }


    #修改支付密码
    public function safepassword(Request $request)
    {
        if ($request->isPost()) {
            $validate = $this->validate($request->post(), '\app\index\validate\UpdatePasswordForm');

            if ($validate !== true) {
                return json(['code' => 1, 'message' => $validate]);
            }
            $user = User::where('id', $this->userId)->find();
            $service = new \app\common\service\Users\Service();
            //判断原密码是否相等
            /*$oldPassword = $request->post('old_pwd');


            $result = $service->checkSafePassword($oldPassword, $user);

            if (!$result) {
                return json(['code' => 1, 'message' => '原密码输入错误']);
            }*/
            $userInfo = $user;
            $form = new RegisterForm();
            if (!$form->checkChange($request->post('code'), $userInfo['mobile'])) {
                return json(['code' => 1, 'message' => '验证码输入错误']);
            }

            //修改
            $user->trad_password = $service->getPassword($request->post('new_pwd'));
            if ($user->save() === false) {
                return json(['code' => 1, 'message' => '修改失败']);
            }

            return json(['code' => 0, 'message' => '修改成功']);
        }

    }
//发送注册验证码
    public function sendPasswordCode(Request $request)
    {
        if ($request->isPost()) {
            $mobile = User::where('id',$this->userId)->value('mobile');
            //检验手机号码
            if (!preg_match('#^1\d{10}$#', $mobile)) {
                return json(['code' => 1, 'message' => '手机号码格式不正确', 'info' => $mobile]);
            }

            $model = new SendCode($mobile, 'change-password');
            if ($model->send()) {
                return json(['code' => 0, 'message' => '你的验证码发送成功']);
            }


            return json(['code' => 1, 'message' => '发送失败']);
        }
    }









    /**
     * 修改个人信息
     */
    public function updateUser(Request $request)
    {
        //获取缓存用户详细信息
        $identity = new Identity();
        $userInfo = $identity->getUserInfo($this->userId);

        $user = new Service();

        $data = array();

        $card = $request->post("card"); //银行卡号
        if ($card) {
            if ($user->checkMsg("card", $card, $userInfo->user_id)) {
                return json(['code' => 1, 'message' => '该银行卡号已经被绑定了']);
            } else {
                $data['card'] = $card;
            }
        }
        $card_name = $request->post("card_name"); //开户行
        if ($card_name) {
            $data['card_name'] = $card_name;
        }
        $zfb = $request->post("zfb"); //支付宝
        if ($zfb) {
            if ($user->checkMsg("zfb", $zfb, $userInfo->user_id)) {
                return json(['code' => 1, 'message' => '该支付宝号已经被绑定了']);
            } else {
                $data['zfb'] = $zfb;
            }
        }
        $zfb_image_url = $request->post("zfb_image_url");

        if ($zfb_image_url) {
            $data['zfb_image_url'] = $zfb_image_url;
        }
        $wx = $request->post("wx"); //微信
        if ($wx) {
            if ($user->checkMsg("wx", $wx, $userInfo->user_id)) {
                return json(['code' => 1, 'message' => '该微信号已经被绑定了']);
            } else {
                $data['wx'] = $wx;
            }
        }
        $wx_image_url = $request->post("wx_image_url");
        if ($wx_image_url) {
            $data['wx_image_url'] = $wx_image_url;
        }
        $real_name = $request->post("real_name"); //真实姓名
        if ($real_name) {
            $data['real_name'] = $real_name;
        }
        $card_id = $request->post("card_id"); //身份证号
        if ($card_id) {
            if ($user->checkMsg("card_id", $card_id, $userInfo->user_id)) {
                return json(['code' => 1, 'message' => '该身份证号已经被绑定了']);
            } else {
                $data['card_id'] = $card_id;
            }
        }
        $card_left = $request->post("card_left"); //身份证反面
        if ($card_left) {
            $data['card_left'] = $card_left;
        }
        $card_right = $request->post("card_right"); //身份证反面
        if ($card_right) {
            $data['card_right'] = $card_right;
        }
        $avatar = $request->post("avatar"); //头像
        if ($avatar) {
            $data['avatar'] = $avatar;
        }
        $money_address = $request->post("money_address"); //头像
        if ($money_address) {
            $data['money_address'] = $money_address;
        }

        $res = \app\common\entity\User::where('id', $this->userId)->update($data);
        // dump(\app\common\entity\User::getLastsql());die;
        if ($res) {
            //更新缓存
            $identity->delCache($this->userId);
            return json(['code' => 0, 'message' => '修改成功']);
        } else {
            return json(['code' => 1, 'message' => '修改失败']);
        }
    }


    /**
     * 清除缓存
     */
    public function delCache()
    {
        $identity = new Identity();
        $identity->delCache($this->userId);
    }



    /**
     * 退出登录
     */
    public function logout()
    {
        User::where('id',$this->userId)->update(['login_time'=>'']);
        $service = new Identity();
        $res = $service->logout();
//        var_dump($res);die;
        return json(['code' => 0, 'msg' => '退出成功']);

//        $this->redirect('pulics/login');
    }

    /**
     * 推广
     */
    public function spread(Request $request)
    {
        //获取当前用户的推广码
        $code = UserInviteCode::where('user_id', $this->userId)->value('invite_code');

        $fileName = Env::get('root_path').'/public/qrcode/' . $code . '.png';
        if (!file_exists($fileName)) {
            if($path = $this->qrcode($code,$request->domain())){
               $this->createImg($path,$code);
            }
        }
        return json(['code' => 0, 'info' => ['path' => '/qrcode/qrcode_' . $code . '.png','code'=>$code]]);
    }
    protected function createImg($path,$code){
        ob_clean();
        header("Content-type: text/html; charset=utf-8");
        $editor = Grafika::createEditor(['Gd']);

        $background =  Env::get('root_path').'/public/GYG/images/yaoqing_bg@2x.png';

        $editor->open($image1, $background);
        $font =  Grafika::fontsDir() . DIRECTORY_SEPARATOR . '微软正黑体.ttf';
        $editor->text($image1 ,$code,30,270,645,new Color("#000000"),$font,0);
        $editor->open($image2, $path);
        $editor->blend($image1, $image2, 'normal', 1, 'top-left', 260, 700);
        $editor->save($image1, Env::get('root_path').'/public/qrcode/qrcode_' . $code . '.png');
    }

    protected function qrcode($code,$domain)
    {

        //logo
        $code_logo = 'images/logo.jpg';
        // 二维码内容
        $qr_code = new QrCode($domain.'/GYG/html/reg.html?code='.$code);
        // 二维码设置
        $qr_code->setSize(230);
        // 边框宽度
        $qr_code->setMargin(10);
        // 图片格式
        $qr_code->setWriterByName('png');
        // 字符编码
        $qr_code->setEncoding('UTF-8');
        // 容错等级，分为L、M、Q、H四级
        $qr_code->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH);
        // 颜色设置，前景色，背景色(默认黑白)
        $qr_code->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
        $qr_code->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
        // 二维码标签
        /*if ($code_font) {
            $qr_code->setLabel('Scan the Code ', 16, __DIR__ . '字体地址', LabelAlignment::CENTER);
        }*/
        // logo设置
        if ($code_logo) {
            $qr_code->setLogoPath($code_logo);
            // logo大小
            $qr_code->setLogoWidth(50);
            // 存放地址
            $code_path = 'qrcode/' . $code . '.png';
            $qr_code->writeFile($code_path);
        } else {
            // 存放地址
            $code_path = 'qrcode/' . $code . '.png';
            $qr_code->writeFile($code_path);
        }
        // 输出图片
        header('Content-Type: ' . $qr_code->getContentType());
        $qr_code->writeString();
        return $code_path;

        /*  ob_clean();
          $logo = 'logo.png';

          $url = $domain.'/gyg/html/register.html?code='.$code;
          $qrCode = new \Endroid\QrCode\QrCode();

          $qrCode->setText($url);
          $qrCode->setSize(400);
          $qrCode->setWriterByName('png');
          $qrCode->setMargin(0);
          $qrCode->setEncoding('UTF-8');
          $qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH);
          $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
          $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 100]);

          $qrCode->setValidateResult(false);

          $qrCode->setLogoPath('/images/logo.png');
          header('Content-Type: ' . $qrCode->getContentType());
          $content = $qrCode->writeString();

          $path ='qrcode/' . $code . '.png';

          file_put_contents($path, $content);



          return $path;*/
    }
    #获取真实姓名
    public function getRealName(){
        $user = User::get(['id',$this->userId]);
        return json(['code' => 0, 'info' => $user['real_name']]);
    }
    #获取绑定信息
    public function getBind(){
        $user = User::where('id',$this->userId)->field('wx,wx_image_url,wx_name,zfb,zfb_image_url,zfb_name,card_name,card_number,card_branch,card_open')->find();
        return json(['code' => 0, 'info' => $user]);
    }
    #绑定收款信息
    public function bind(Request $request){
        $type = $request->post('type');
        $user = User::get(['id',$this->userId]);
        $form = new RegisterForm();
        if (!$form->checkChange($request->post('code'), $user['mobile'])) {
             return json(['code' => 1, 'message' => '验证码输入错误']);
        }
        $data = [];
        switch ($type){
            case 'wx':
                $data['wx'] = $request->post('wx');
                $data['wx_name'] = $request->post('wx_name');
                $data['wx_image_url'] = $request->post('wx_image_url');
                $data['wx_name'] = $request->post('wx_name');
                break;
            case 'zfb':
                $data['zfb'] = $request->post('zfb');
                $data['zfb_name'] = $request->post('zfb_name');
                $data['zfb_image_url'] = $request->post('zfb_image_url');
                $data['zfb_name'] = $request->post('zfb_name');
                break;
            case 'bank':
                $data['card_name'] = $request->post('card_name');
                $data['card_number'] = $request->post('card_number');
                $data['card_branch'] = $request->post('card_branch');
                $data['card_open'] = $request->post('card_open');
                break;

        }
        $userMdel = new User();
        $update = $userMdel->where('id',$this->userId)->update($data);

        if($update){
            return json(['code' => 0, 'message' => '修改成功']);
        }else{
            return json(['code' => 1, 'message' => '修改失败']);
        }
    }


    #客服
    public function serviceInfo(){
        $info = ServiceInfo::find();

        return json(['code' => 0, 'info'=>$info]);
    }

    public function getAttest(){

        $info = RealPass::where('user_id',$this->userId)->where('status',2)->find();
        if(!$info){
            $info = RealPass::where('user_id',$this->userId)->where('status',1)->find();
        }
        if(!$info){
            $info = RealPass::where('user_id',$this->userId)->where('status',3)->find();
        }
        if(!$info){
            $info = [];
        }
        return json(['code' => 0, 'info' =>$info]);
    }

    #实名认证
    public function attest(Request $request)
    {
        if ($request->isPost()) {

            $validate = $this->validate($request->post(), '\app\index\validate\Attest');
            $uid = $this->userId;

            if ($validate !== true) {
                return json(['code' => 1, 'message' => $validate]);
            }

            $card_id = $request->post('card_id');
            $preg_card = '/^([\d]{17}[xX\d]|[\d]{15})$/';
            if (!preg_match($preg_card, $card_id)) {
                return json(['code' => 1, 'message' => '身份证格式不对']);
            }
            $userModel = new User();
            if ($userModel->checkMsg("card_id", $card_id, $uid)) {
                return json(['code' => 1, 'message' => '该身份证号已经被绑定了']);
            }



           /* $user = User::where('id', $this->userId)->find();
            if ($user['real_pass'] == 2) {
                return json(['code' => 1, 'message' => '已认证']);

            }*/


            $is_ins_realpass = RealPass::where('user_id', $uid)->where('status', 2)->find();
            if ($is_ins_realpass) {
                return json(['code' => 1, 'message' => '已实名']);
            }

            $is_ins_realpass = RealPass::where('user_id', $uid)->where('status', 1)->find();
            if ($is_ins_realpass) {
                return json(['code' => 1, 'message' => '审核已提交,请耐心等待']);
            }


            $data = [
                'card_id' => $request->post('card_id'),
                'real_name' => $request->post('real_name'),
                'real_pass' => '3',
                'card_right' => $request->post('card_right'),
                'card_left' => $request->post('card_left'),
            ];
            $insPass = [
                'user_id' => $uid,
                'real_name' => $request->post('real_name'),
                'card_id' => $request->post('card_id'),
                'card_right' => $request->post('card_right'),
                'card_left' => $request->post('card_left'),
                'status' => 1,
                'create_time' => time()

            ];

            Db::startTrans();
            try {
                RealPass::insert($insPass);
                $msg = '认证审核中，48小时内后台完成审核。';
                User::where('id', $uid)->update($data);

                Db::commit();
                return json(['code' => 0, 'message' => $msg]);
            } catch (\Exception $e) {

                Db::rollback();
                return json(['code' => 1, 'message' => '修改失败']);
            }

        }
        if ($request->isGet()) {

            $list = User::where('id', $this->userId)->field('mobile,real_name,card_id,card_right,card_left,real_pass')->find();
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);
        }

    }


    #可用资产
    public function gyg(Request $request){
        $status = $request->post('status',1);
        $gyg = Mywallet::where('user_id',$this->userId)->value('gyg');
        $list = MywalletLog::where('user_id',$this->userId)->where('money_type',1)->where('status',$status)->order('id desc')->paginate(15, false, [
            'query' => isset($map) ? $map : []
        ]);
        return json(['code' => 0, 'message' => '获取成功', 'info' => ['gyg'=>$gyg,'list'=>$list]]);
    }
    #锁仓
    public function lock(Request $request){
        $status = $request->post('status',1);
        $lock = Mywallet::where('user_id',$this->userId)->value('lock');
        $list = MywalletLog::where('user_id',$this->userId)->where('money_type',2)->where('status',$status)->order('id desc')->paginate(15, false, [
            'query' => isset($map) ? $map : []
        ]);
        return json(['code' => 0, 'message' => '获取成功', 'info' => ['lock'=>$lock,'list'=>$list]]);
    }
    #互转
    public function transfer(Request $request){
        $mobile = $request->post('mobile');
        $num = $request->post('num');
        $trad_password = $request->post('trad_password');
        if(!$mobile || !$num || !$trad_password){
            return json(['code' => 1, 'message' => '请求参数错误']);
        }

        if($num < Config::getValue('transfer_min')){
            return json(['code' => 1, 'message' => '最小转账数额'.Config::getValue('transfer_min')]);
        }

        $touser = User::where('mobile',$mobile)->find();
        if(!$touser){
            return json(['code' => 1, 'message' => '用户不存在']);
        }

        $user = User::where('id', $this->userId)->find();
        $service = new \app\common\service\Users\Service();
        $result = $service->checkSafePassword($trad_password, $user);
        if (!$result) {
            return json(['code' => 1, 'message' => '支付密码输入错误']);
        }

        $add = MywalletLog::addLog( $this->userId, $num, 1, '互转', 5, 2);
        if($add){
            MywalletLog::addLog( $touser['id'], bcmul($num,bcmul(100-Config::getValue('transfer'),0.01,4),4), 1, '互转', 5, 1);
            return json(['code' => 0, 'message' => '转账成功']);
        }
        return json(['code' => 1, 'message' => '转账失败']);
    }


    public function my_temp(Request $request){
        $direct = User::where('pid',$this->userId)->count();
        $userList = User::select();
        $temp = self::getTemp($userList,$this->userId);

        $uid = $request->post('uid',$this->userId);
        if($mobile = $request->post('mobile')){
            $list = User::where('pid',$uid)->where('mobile','like','%'.$mobile.'%')->field('id,mobile,register_time')->select();
        }else{
            $list = User::where('pid',$uid)->field('id,mobile,register_time')->select();
        }
        foreach($list as $k=>$v){
            $list[$k]['create_time'] = $v['register_time'];//strtotime('Y-m-d H:i:s');
        }
        $info = [
            'direct'=>$direct,
            'temp'=>count($temp),
            'list'=>$list,
        ];
        return json(['code' => 0, 'message' => '获取成功','info'=>$info]);
    }

    public function help(){
        $info =Db::name('help')->where('id',1)->find();
        $info['content'] = htmlspecialchars_decode($info['content']);
        return json(['code' => 0, 'message' => '获取成功','info'=>$info]);
    }

    #地址列表
    public function ercAddressList(Request $request){
        $list =  Db::name('user_erc')->where('user_id',$this->userId)->paginate(15, false, [
            'query' => isset($map) ? $map : []
        ]);
        return json(['code' => 0, 'message' => '获取成功','info'=>$list]);
    }
    #地址
    public function ercAddress(Request $request){
        $info = Db::name('user_erc')->where('id',$request->post('id'))->field('erc_name,erc_adderss')->find();
        return json(['code' => 0, 'message' => '获取成功','info'=>$info]);
    }
    #添加地址
    public function insertErcAddress(Request $request){
        $user = User::where('id',$this->userId)->find();
        //密码
        if(!( (new Service())->checkPayPassword($request->post('trad_password'),$user)) ){
            return json(['code'=>1,'msg'=>'支付密码错误']);
        }

        $data = [
            'user_id' => $this->userId,
            'erc_name' => $request->post('erc_name'),
            'erc_adderss' => $request->post('erc_adderss'),
            'create_time' => time(),
        ];
        $update =Db::name('user_erc')->insert($data);
        if($update){
            return json(['code' => 0, 'message' => '操作成功']);
        }
        return json(['code' => 1, 'message' => '操作失败']);
    }
    #修改地址
    public function updateErcAddress(Request $request){
        $user = User::where('id',$this->userId)->find();
        //密码
        if(!( (new Service())->checkPayPassword($request->post('trad_password'),$user)) ){
            return json(['code'=>1,'msg'=>'支付密码错误']);
        }

        $data = [
            'erc_name' => $request->post('erc_name'),
            'erc_adderss' => $request->post('erc_adderss'),
            'update_time' => time(),
        ];
        $update = Db::name('user_erc')->where('id',$request->post('id'))->update($data);
        if($update){
            return json(['code' => 0, 'message' => '操作成功']);
        }
        return json(['code' => 1, 'message' => '操作失败']);
    }
    #删除地址
    public function deleteErcAddress(Request $request){
        $update = Db::name('user_erc')->where('id',$request->post('id'))->delete();
        if($update){
            return json(['code' => 0, 'message' => '操作成功']);
        }
        return json(['code' => 1, 'message' => '操作失败']);
    }
    #我的资产
    public function myWallet(Request $request){
        $gyg = Mywallet::where('user_id',$this->userId)->value('gyg');
        $log = MywalletLog::where('user_id',$this->userId)->where('types',8)->order('id desc')->paginate(15, false, [
            'query' => isset($map) ? $map : []
        ]);
        return json(['code' => 0, 'message' => '获取成功','info'=>['gyg'=>$gyg,'list'=>$log]]);
    }
    #提币页面
    public function withdraw(Request $request){
        $adderss =  Db::name('user_erc')->where('user_id',$this->userId)->select();
        $gyg = Mywallet::where('user_id',$this->userId)->value('gyg');
        $fee = Config::getValue('trans_ratio');
        $ratio = Proportion::getToday();
        return json(['code' => 0, 'message' => '获取成功','info'=>['adderss'=>$adderss,'gyg'=>$gyg,'fee'=>$fee,'ratio'=>$ratio,'withdraw_base'=>Config::getValue('withdraw_base')]]);
    }
    #提币
    public function doWithdraw(Request $request){
        $user = User::where('id',$this->userId)->find();
        //密码
        if(!( (new Service())->checkPayPassword($request->post('trad_password'),$user)) ){
            return json(['code'=>1,'msg'=>'支付密码错误']);
        }
        if(!is_int($request->post('num') / Config::getValue('withdraw_base'))){
            return json(['code' => 1, 'message' => '提币数量必须是'.Config::getValue('withdraw_base').'的整倍数']);
        }
        $entity = new WithdrawMoney();
        $entity->user_id = $this->userId;
        $entity->address = $request->post('address');
        $entity->num = $request->post('num');
        $entity->ratio_money = Config::getValue('trans_ratio');
        $entity->ratio = Proportion::getToday();
        $entity->create_time = time();

        if($entity->save()){
            MywalletLog::addLog($this->userId, $request->post('num'), 1, '提币', 8, 2);
            return json(['code' => 0, 'message' => '操作成功，请耐心等待审核']);
        }
        return json(['code' => 1, 'message' => '操作失败']);
    }

    public function is_real_pass(){
        $user = User::where('id',$this->userId)->value('real_pass');
        if ($user != 2) {
            return json(['code' => 1, 'message' => '请先实名认证']);
        }
        return json(['code' => 0, 'message' => '已实名']);
    }


    //获取互转手续费
    public function get_transfer(){
        return json(['code' => 0, 'message' => '获取成功','info'=>Config::getValue('transfer')]);
    }
    //根据手机号获取昵称
    public function getNicknameByMobile(Request $request){
        $nick_name = User::where('mobile',$request->post('mobile'))->value('nick_name');
        return json(['code' => 0, 'message' => '获取成功','info'=>$nick_name]);
    }
}
